package com.idealighter.game.roomcard.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.core.assertions.HuohuaAssert;
import com.idealighter.game.core.common.Game;
import com.idealighter.game.core.error.ErrorCode;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.player.manager.PlayerMgr;
import com.idealighter.game.player.struct.Player;
import com.idealighter.game.roomcard.bo.RoomCardRoomListDomainBo;
import com.idealighter.game.roomcard.common.AbstractRoomCardRoom;
import com.idealighter.game.roomcard.common.AbstractRoomCardRoomWithTable;
import com.idealighter.game.roomcard.message.ResRoomCardListMsg;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardGroupMemberDomain;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardGroupMemberDomainExample;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardRoomListDomain;
import com.idealighter.game.server.db.datadb.mapper.RoomCardGroupMemberMapper;
import com.idealighter.utils.check.EmptyUtil;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
public class RoomCardMgr {
  private static final Logger LOG = LoggerFactory.getLogger(RoomCardMgr.class);
  // @Inject
  // private ScheduleMgr scheduleMgr;
  @Inject
  private PlayerMgr playerMgr;
  // // 事件管理器
  // private final EventMgr eventMgr;
  // private RoomCardGameCfgMgrScript roomCardGameCfgMgrScript;
  private RoomCardRoomListMgrScript roomCardRoomListMgrScript;
  // private ExecutorMgr executorMgr;
  private RoomCardGroupMemberMapper roomCardGroupMemberMapper;
  @Inject
  private RoomCardMsgMgr roomCardMsgMgr;

  /**
   * 构造函数.
   */
  @Inject
  public RoomCardMgr(// EventMgr eventMgr, ExecutorMgr executorMgr,
      // RoomCardGameCfgMgrScript roomCardGameCfgMgrScript,
      RoomCardRoomListMgrScript roomCardRoomListMgrScript,
      RoomCardGroupMemberMapper roomCardGroupMemberMapper) {
    // this.eventMgr = eventMgr;
    // this.roomCardGameCfgMgrScript = roomCardGameCfgMgrScript;
    this.roomCardRoomListMgrScript = roomCardRoomListMgrScript;
    this.roomCardGroupMemberMapper = roomCardGroupMemberMapper;
    // this.executorMgr = executorMgr;
    // eventMgr.register(this);
  }

  /**
   * @Title enterRoomCard.
   * @Description 进入房卡
   * @date 2018年5月17日 下午8:14:40
   * @param player 玩家
   * @param roomId 房卡编号
   * @return .
   */
  public ResMessage enterRoomCard(Player player, Long roomId) {
    AbstractRoomCardRoom room = roomCardRoomListMgrScript.findRoomCard(roomId);
    HuohuaAssert.isTrue(room != null, ErrorCode.ROOMCARD_NOT_EXIST);
    Long playerId = player.playerBo().getId();
    // 校验是否已经进入房卡
    if (room.hasPlayer(playerId)) {
      LOG.error("玩家[{}][{}]已经进入房间[{}]不能重复进入", playerId, player.playerBo().getPlayerName(),
          room.getId());
      HuohuaAssert.isTrue(false, ErrorCode.ROOMCARD_ALREADY_IN);
    }
    // 如果房卡属于群，判断是否加入群
    int groupId = room.getGroupId();
    if (groupId > 0) {
      RoomCardGroupMemberDomainExample example = new RoomCardGroupMemberDomainExample();
      example.createCriteria().andGroupIdEqualTo(groupId).andPlayerIdEqualTo(playerId);
      example.setLimit(1);
      List<RoomCardGroupMemberDomain> list = roomCardGroupMemberMapper.selectByExample(example);
      if (EmptyUtil.listIsEmpty(list)) {
        LOG.error("玩家[{}][{}]未加入群[{}],不能进入房间[{}]", playerId, player.playerBo().getPlayerName(),
            groupId, room.getId());
        HuohuaAssert.isTrue(false, ErrorCode.ROOMCARD_ENTER_ERROR, "非群" + groupId + "成员");
      }
    }
    Game game = room.game();
    return roomCardMsgMgr.enterRoomCardMsg(player, game, false);
  }

  /**
   * @Title roomCardList.
   * @Description 分页获取房卡列表
   * @date 2018年6月8日 下午1:54:25
   * @param player 玩家
   * @param type 类型(0:我加入的,1:属于群的,2:特定群的)
   * @param lastRoomId 用于分页
   * @param groupId 群编号
   * @param pageSize 每页大小
   * @return .
   */
  public ResRoomCardListMsg roomCardList(Player player, int type, long lastRoomId, int groupId,
      int pageSize) {
    List<AbstractRoomCardRoom> list =
        roomCardRoomListMgrScript.findRoomCardList(player, type, lastRoomId, groupId, pageSize);

    List<RoomCardRoomListDomainBo> domainList = new ArrayList<>();
    if (EmptyUtil.listIsNotEmpty(list)) {
      for (AbstractRoomCardRoom item : list) {
        @SuppressWarnings("unchecked")
        AbstractRoomCardRoomWithTable<?, ?, ? extends RoomCardRoomListDomain, ?> domainItem =
            (AbstractRoomCardRoomWithTable<?, ?, ? extends RoomCardRoomListDomain, ?>) item;
        RoomCardRoomListDomainBo resultItem = new RoomCardRoomListDomainBo();
        resultItem.setDomain(domainItem.getRoomCfg());
        resultItem.setOwnerName(playerMgr.selectPlayer(item.getOwnerId()).getPlayerName());
        resultItem.setPlayerNum(item.getSeatDownNum().get());
        domainList.add(resultItem);
      }
    }

    return roomCardMsgMgr.roomCardListMsg(player, domainList);
  }
}
